Skip to content

Conversation

ExtremeGTX
Copy link
Contributor

@ExtremeGTX ExtremeGTX commented Mar 10, 2025

Implement a basic version of USB MTP (Media transfer protocol) class which support
the necessary MTP commands to handle Dir/file transfer between a device and host.
Fixes: #54468

TODO:

  • Address FIXME comment(s): mainly handle cancel transaction request.
  • Test on USB Full speed (rpi_pico)
  • Test on another board than STM32F769i-DISCO
    • rpi_pico 2040
  • Remove Extensive logging
  • Make MAX_PACKET_SIZE value dependent on whether USB is HS/FS
  • Polishing the MTP Sample including updating the readme file
  • Improve Error handling

Thanks:

  • Zephyr USB Team for helping me understand how device_next works
  • HHD Software for supporting this project.

@ExtremeGTX
Copy link
Contributor Author

Dear Reviewers:

Could you please help me validate the current architecture first, then we can move to final polishing when I get the PR out of draft state.

Thanks a lot.

@ExtremeGTX ExtremeGTX force-pushed the usb_mtp_next_PR branch 2 times, most recently from 7874f45 to 748eb7e Compare March 10, 2025 03:37
@ExtremeGTX ExtremeGTX requested a review from kartben March 14, 2025 20:11
@ExtremeGTX ExtremeGTX force-pushed the usb_mtp_next_PR branch 2 times, most recently from f6bfa5e to 02a2fb9 Compare April 21, 2025 21:03
@ExtremeGTX ExtremeGTX requested a review from jfischer-no April 21, 2025 21:06
@tmon-nordic
Copy link
Contributor

The class fixup/polishing commits should be squashed together into the main MTP class implementation commit. Sample changes should go into separate commits.

@kartben kartben requested a review from Copilot April 24, 2025 01:01
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements basic USB MTP class support to enable file and directory transfers between a device and a host. Key changes include:

  • Adding a new MTP class header with necessary function declarations and context structure.
  • Providing a sample application to test and demonstrate the MTP functionality.
  • Updating USB descriptors, DTS bindings, and sample configuration to support MTP.

Reviewed Changes

Copilot reviewed 7 out of 18 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
subsys/usb/device_next/class/usbd_mtp_class.h Introduces declarations for the USB MTP class support
samples/subsys/usb/mtp/src/main.c Implements a sample application to test the MTP class driver
samples/subsys/usb/mtp/sample.yaml Adds a sample configuration for the MTP device class
include/zephyr/usb/usb_ch9.h Updates USB descriptors to include the MTP image class type
dts/bindings/fs/zephyr,fstab-common.yaml Adds a filesystem binding property for enabling MTP file access
Files not reviewed (11)
  • samples/subsys/usb/mtp/CMakeLists.txt: Language not supported
  • samples/subsys/usb/mtp/Kconfig: Language not supported
  • samples/subsys/usb/mtp/README.rst: Language not supported
  • samples/subsys/usb/mtp/boards/rpi_pico.overlay: Language not supported
  • samples/subsys/usb/mtp/boards/stm32f769i_disco.overlay: Language not supported
  • samples/subsys/usb/mtp/prj.conf: Language not supported
  • subsys/usb/device_next/CMakeLists.txt: Language not supported
  • subsys/usb/device_next/class/Kconfig: Language not supported
  • subsys/usb/device_next/class/Kconfig.mtp: Language not supported
  • tests/subsys/usb/device_next/build_all.conf: Language not supported
  • tests/subsys/usb/device_next/build_all.overlay: Language not supported

@jfischer-no jfischer-no removed the request for review from kartben April 24, 2025 08:39
@jfischer-no jfischer-no self-assigned this Apr 24, 2025
@kartben kartben self-requested a review April 24, 2025 20:34
@ExtremeGTX ExtremeGTX force-pushed the usb_mtp_next_PR branch 2 times, most recently from 3bb7378 to 9910433 Compare April 24, 2025 21:51
@ExtremeGTX ExtremeGTX marked this pull request as ready for review April 24, 2025 21:51
@ExtremeGTX
Copy link
Contributor Author

Rebase update:

  • Fix SonarQube issues where it makes sense.

@ExtremeGTX
Copy link
Contributor Author

@nashif @tmon-nordic any idea why strnlen is not available in twister -T tests/subsys/usb/device_next/ ?

@DenisD3D
Copy link

DenisD3D commented Jul 6, 2025

Hello,
I gave your PR a shoot on my stm32l562e_dk board. It include a 512Mb mx25l51245g flash i wanted to use (The littlefs sample works).

First, I had to increase CONFIG_USBD_THREAD_STACK_SIZE to 4096 not to get stack overflows.
Also now I'm getting this error when I connect the usb cable to the board.

[00:00:00.455,000] <inf> usb_mtp_sample: USBD message: New device configuration
[00:00:00.536,000] <err> os: ***** BUS FAULT *****
[00:00:00.536,000] <err> os:   Precise data bus error
[00:00:00.536,000] <err> os:   BFAR Address: 0xc
[00:00:00.536,000] <err> os: r0/a1:  0x20000258  r1/a2:  0x00000000  r2/a3:  0x20008920
[00:00:00.536,000] <err> os: r3/a4:  0x20002a54 r12/ip:  0x20005158 r14/lr:  0x080051d9
[00:00:00.536,000] <err> os:  xpsr:  0x61000000
[00:00:00.536,000] <err> os: Faulting instruction address (r15/pc): 0x08005894
[00:00:00.536,000] <err> os: >>> ZEPHYR FATAL ERROR 25: Unknown error on CPU 0
[00:00:00.536,000] <err> os: Current thread: 0x20002120 (unknown)
[00:00:00.625,000] <err> os: Halting system

Using a debugger, I traced it to the line struct mtp_container *mtp_command = (struct mtp_container *)buf_in->data; in usbd_mtp_class.c where buf_in appear to be null.
I'm using a Linux Fedora 42 and the board USB C port. It successfully appear in the dmesg log but then crash.

[17728.372682] usb 1-1: new full-speed USB device number 28 using xhci_hcd
[17728.498019] usb 1-1: New USB device found, idVendor=2fe3, idProduct=0009, bcdDevice= 4.02
[17728.498023] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[17728.498024] usb 1-1: Product: USBD MTP sample
[17728.498026] usb 1-1: Manufacturer: Zephyr Project
[17728.498027] usb 1-1: SerialNumber: 2034354E323650030010000B

I'm not export in the usb protocol so not really sure what to test now.
Denis

@ExtremeGTX
Copy link
Contributor Author

@DenisD3D Thanks for your feedback.
Could you please add the following KCONFIGs to your project/mtp_sample
And share full logs from booting till the crash ?
Thank you!

@Finomnis
Copy link
Contributor

Finomnis commented Jul 8, 2025

I think you forgot to attach the actual kconfig values :)

@ExtremeGTX
Copy link
Contributor Author

ExtremeGTX commented Jul 9, 2025

I think you forgot to attach the actual kconfig values :)

Sorry 🤦, here you go

CONFIG_USBD_LOG_LEVEL_DBG=y
CONFIG_USBD_MTP_LOG_LEVEL_DBG=y

@DenisD3D
Copy link

DenisD3D commented Jul 9, 2025

Hello,
Thanks for investigating
Here are the full logs from my stm32l562e_dk: mtp_logs.txt
Denis

@DenisD3D
Copy link

Hi @ExtremeGTX, did you had time to take a look on this ? Thanks

Copy link

@ExtremeGTX
Copy link
Contributor Author

Hi @ExtremeGTX, did you had time to take a look on this ? Thanks

Hi @DenisD3D
Since I'm away from my office, I wasn't able to reproduce it.

But I have added 2 things with latest push

  • Get the right packet size based on USB Speed.
  • Added extra debug messages to the case you have reported so i can get better insights.

@DenisD3D
Copy link

Thanks for the new version!
Here are the new logs with the extra debug mtp_logs_v2.txt
Thanks, Denis

@ExtremeGTX
Copy link
Contributor Author

Denis helped me find some compatibility issues with Linux, I'm working on them along with some improvements.

@Finomnis
Copy link
Contributor

Finomnis commented Aug 7, 2025

Any news?

@Finomnis
Copy link
Contributor

Thread safety so far completely missing from what I can tell

@zephyrbot zephyrbot added area: Devicetree Bindings area: Tests Issues related to a particular existing or missing test area: Boards/SoCs labels Sep 29, 2025
@ExtremeGTX
Copy link
Contributor Author

ExtremeGTX commented Sep 29, 2025

Changes:

  • Fix linux (libmtp) compatibility
  • Fix calculation of the remaining bytes count in GET_OBJECT
  • Improve error handling
  • Refactor duplicated code of sending MTP responses
  • MTP State machine improvements
  • Test on Android in addition to Windows/Linux

Warning

Testing on latest Zephyr on STM32 and RPI Pico wasn't possible due to these issues
STM32: #94916 (USB regression)
RPi Pico: #96224 (flash driver regression)
I'm testing MTP implementation on 1ea0267, mainly using STM32F769i_DISCO

Implement a basic version of USB MTP (Media transfer protocol) class
which support the necessary MTP commands to handle Dir/file
transfer between a device and host.

Signed-off-by: Mohamed ElShahawi <[email protected]>
Add a sample application that demonstrates how to use the new USB MTP
device class. This shows how to set up the device tree to expose
storage partitions to a host over USB using MTP.
Files/Dirs can be accessed from host without host supportfor the
underlying filesystem like littlefs.

Signed-off-by: Mohamed ElShahawi <[email protected]>
Copy link

sonarqubecloud bot commented Oct 6, 2025

@ExtremeGTX
Copy link
Contributor Author

ExtremeGTX commented Oct 6, 2025

For Linux users: whenever you face a problem, please try to reproduce with logs enabled by following these steps:

  • Zephyr:
    • Enable USB and MTP Debugging logs
      CONFIG_USBD_LOG_LEVEL_DBG=y
      CONFIG_USBD_MTP_LOG_LEVEL_DBG=y
      
  • Linux (Distros which uses libmtp and GVFS):
    • Start a terminal and run Note: This will kill all nautilus and gvfs sessions: pkill nuatilus; pkill gvfs; GVFS_GPHOTO2_DEBUG=all GVFS_MTP_DEBUG=all GVFS_DEBUG=1 $(find /usr/lib* -name gvfsd 2> /dev/null) --replace 2>&1 | tee gvfsd.log
    • Inspect the created log file gvfsd.log for your privacy.
    • Upload Zephyr terminal output and gvfsd.log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support the USB MTP feature.
9 participants